package com.chujian.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chujian.dao.FoodDao;
import com.chujian.dao.FoodListDao;
import com.chujian.daoImpl.FoodDaoImpl;
import com.chujian.daoImpl.FoodListDaoImpl;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
//		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
//		获取客户端发送过来的数据
		String categary=request.getParameter("categary");
		String foodListId=request.getParameter("foodListId");
		String food_name=request.getParameter("food_name");
		String foodName=new String(food_name.getBytes("iso-8859-1"),"UTF-8");
		System.out.println(categary);
		System.out.println(foodListId);
		System.out.println(foodName);
		int food_list_id=-1;
		try {
			food_list_id=Integer.parseInt(foodListId);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ;
		}
		FoodListDao dao=new FoodListDaoImpl();
		FoodDao foodDao=new FoodDaoImpl();
		boolean result=dao.commentFood(categary, food_list_id);
		
			if(categary.equals("zan")){
				if(result){
					foodDao.commentFood(foodName, "zan");
					out.write("赞+1");
				}else{
					out.write("点赞失败");
				}
				
				
			}else if(categary.equals("cai")){
				if(result){
					foodDao.commentFood(foodName, "cai");
					out.write("踩+1");
				}else{
					out.write("点踩失败");
				}
			
		}
		
		
		
		
	}

}
